/**
 * 
 */
package com.hk.Mgr.web.controller.cooperation;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.cooperation.CompanyReq;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.entity.product.Company;
import com.hk.service.product.ICompany;

/**
 * @author psx 产品信息实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/companyCtrl")
public class CompanyCtrl extends BaseCtrl {

	@Autowired
	private ICompany companyService;

    /**
     * @param
     * @Title: getProductInfoList
     * @Description: 分页获取系统模块
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseBase<List<Company>> getProductInfoList(
            @RequestBody CompanyReq req) {
        ResponseBase<List<Company>> resp = new ResponseBase<List<Company>>();
        HqlFilter hqlFilter = new HqlFilter();
        //公司名
        if (StringUtils.isNotEmpty(req.getName())) {
            hqlFilter.addEQFilter("name", req.getName());
        }
        //公司id
        if (req.getId() != null) {
            hqlFilter.addEQFilter("id", req.getId());
        }

        hqlFilter.addSort("id");
        hqlFilter.addOrder("desc");

        try {
            List<Company> companyList = companyService.findByFilter(hqlFilter, req.getCurPage(), req.getPageSize());
            Long totalSize = companyService.countByFilter(hqlFilter);

            resp.setSize(totalSize);
            resp.setResultCode(StatusCode.SUCCESS.key());
            resp.setResult(companyList);

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            resp.setMsg("操作异常，请重试");
            resp.setResultCode(StatusCode.EXCEPTION.key());
        }

        return resp;
    }

	/**
	 * @Title: saveProductInfo
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ResponseBase<Boolean> saveCompany(
			@RequestBody Company com) {
		ResponseBase<Boolean> resp = new ResponseBase<>();
		
		Integer id = null;
		if (com != null) {
			id = companyService.add(com);
		}
		
		if (id != null){
			resp.setResult(true);
			resp.setMsg("添加失败！");
			resp.setResultCode(StatusCode.SUCCESS.key());
		}else{
			resp.setMsg("添加成功！");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
	
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseBase<Boolean> updCompany(
			@RequestBody Company com) {
		ResponseBase<Boolean> resp = new ResponseBase<>();
		
		if(com == null || com.getId() == null){
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			resp.setMsg("参数不对：id is null！");
			resp.setResultCode(StatusCode.SUCCESS.key());
			return resp;
		}else{
			com.setUpts(System.currentTimeMillis());
			companyService.update(com);
			resp.setResult(true);
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	 
}
